#include "jointsliderwidget.h"

#include <QScroller>
#include <QTimer>
#include "models/command.h"
#include "qdebug.h"
// DirectionButton 实现
DirectionButton::DirectionButton(bool isLeft, QWidget *parent)
    : TouchButton(parent)
    , m_isLeft(isLeft)
    , m_step(1)
{
    setText("");
    setFixedSize(60*g_xScale, 60*g_xScale);
    setStyleSheet(
        QString(
            "QPushButton {"
            "    border: none;"
            "    border-image: url(:/images/control_from/%1);"
            "    background-color: transparent;"
            "}"
            "QPushButton:pressed {"
            "    border: none;"
            "    border-image: url(:/images/control_from/%2);"
            "    background-color: transparent;"
            "}"
            ).arg(isLeft ? "left_btn.png" : "right_btn.png")
            .arg(isLeft ? "left_pressed.png" : "right_pressed.png")
        );
    connect(this, &QPushButton::clicked, this, &DirectionButton::onClicked);
    connect(this, &QPushButton::pressed, this, &DirectionButton::onPressed);
    connect(this, &QPushButton::released, this, &DirectionButton::onReleased);
}

void DirectionButton::onClicked()
{
    emit stepClicked(m_isLeft);
}

void DirectionButton::onPressed()
{
    emit stepPressed(m_isLeft);
}
void DirectionButton::onReleased()
{
    emit stepReleased(m_isLeft);
}

// JointSliderWidget 实现
JointSliderWidget::JointSliderWidget(int w, int h,QWidget *parent)
    : QWidget(parent)
{
    setFixedSize(w,h);
    initUI();
    setupConnections();
    QScroller::grabGesture(this, QScroller::LeftMouseButtonGesture);
}

void JointSliderWidget::init(int nJointId, QString strName, QString strUnit, float min, float max, float value)
{
    m_bInitDest = true;
    m_jointId = nJointId;
    m_strName = strName;
    m_strUnit = strUnit;
    m_fCurValue = value;
    m_slider->setFloatRange(min, max);
    m_slider->setFloatValue(m_fCurValue);
    m_destLabel->setText(QString::number(m_fCurValue, 'f', 1)+m_strUnit);
    m_bInitDest = false;
}

void JointSliderWidget::initUI()
{
    setStyleSheet("background-color: #2A2A2A;");
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 5*g_xScale,0, 5*g_xScale);
    mainLayout->setSpacing(5*g_xScale);

    // 创建顶部名称标签
    m_nameLabel = new QLabel(this);
    m_nameLabel->setStyleSheet(QString(
        "QLabel {"
        "    color: white;"
        "    font-size:%1px;"
        "    background-color: transparent;"
        "    font-family: '微软雅黑';"
                                   "padding-left:%2px"
        "}").arg(int(18*g_xScale)).arg(int(10*g_xScale)));
    //m_nameLabel->setAlignment(Qt::AlignHCenter);

    mainLayout->addWidget(m_nameLabel);
    m_nameLabel->setText(m_strName);
    // 创建底部控制布局
    QHBoxLayout *controlLayout = new QHBoxLayout;
    controlLayout->setContentsMargins(0, 0, 0, 0);
    controlLayout->setSpacing(1);

    // 创建控件
    m_leftBtn = new DirectionButton(true, this);
    m_rightBtn = new DirectionButton(false, this);


    QHBoxLayout *sliderLayout = new QHBoxLayout;
    sliderLayout->setContentsMargins(0, 0, 0, 0);
    sliderLayout->setSpacing(0);
    m_slider = new CustomSlider(this);
    m_destLabel = new QLabel("0.0");
    // 设置滑块样式和图标
    m_slider->setFixedSize(width()-232*g_xScale, 60*g_xScale);
    m_destLabel->setFixedSize(70*g_xScale, 60*g_xScale);
    m_destLabel->setAlignment(Qt::AlignCenter);
    m_destLabel->setStyleSheet(
        QString(
            "QLabel { \
            color: white; \
            border-image: none; \
            background: transparent;\
            border-image: url(:/images/control_from/background_label.png);}"
            ));
    QFont font = this->font();
    font.setPixelSize(16*g_xScale);
    font.setFamily("微软雅黑");
    m_destLabel->setFont(font);
    // 添加到控制布局
    sliderLayout->addWidget(m_slider);
    sliderLayout->addWidget(m_destLabel);
    controlLayout->addWidget(m_leftBtn);
    controlLayout->addLayout(sliderLayout);
    controlLayout->addWidget(m_rightBtn);
    mainLayout->addLayout(controlLayout);
    m_slider->setEnabled(false);//不允许使用滑块操作
}

void JointSliderWidget::setupConnections()
{
    connect(m_leftBtn, &DirectionButton::stepPressed, this,[this](bool isLeft) {
        Q_UNUSED(isLeft);
        //m_slider->setFloatValue(m_slider->floatValue() - m_fStep);

        qDebug()<<m_slider->floatValue()<<m_fStep;
        emit start(m_jointId,m_slider->floatValue() - m_fStep);
    });
    connect(m_rightBtn, &DirectionButton::stepPressed, this,[this](bool isLeft) {
        Q_UNUSED(isLeft);
        //m_slider->setFloatValue(m_slider->floatValue() + m_fStep);

        emit start(m_jointId,m_slider->floatValue() + m_fStep);
    });
    connect(m_leftBtn, &DirectionButton::stepReleased, this,[this](bool isLeft) {
        Q_UNUSED(isLeft);
        emit stop(m_jointId);
    });
    connect(m_rightBtn, &DirectionButton::stepReleased, this,[this](bool isLeft) {
        Q_UNUSED(isLeft);
        emit stop(m_jointId);
    });
    connect(m_slider, &CustomSlider::floatValueChanged, this, [this](float value) {
        // if(value != m_fCurValue && !m_bInitDest)
        //     emit start(m_jointId,value);
        //m_destLabel->setText(QString::number(value, 'f', 1)+m_strUnit);
    });
    connect(m_slider, &CustomSlider::sliderReleased, this, [this]() {
        emit stop(m_jointId);
    });
}

void JointSliderWidget::setCurValue(float value)
{
    m_fCurValue = value;
    m_destLabel->setText(QString::number(m_fCurValue,'f',1));
    m_slider->setFloatValue(m_fCurValue);
}

float JointSliderWidget::floatValue() const
{
    return m_slider->floatValue();
}

void JointSliderWidget::setStep(float step)
{
    m_fStep = step;
}




